﻿//<TunynetCopyright>
//--------------------------------------------------------------
//<copyright>青岛拓宇网络科技有限公司 2005-2008</copyright>
//<version>V0.5</verion>
//<createdate>2008-2-21</createdate>
//<author>mazq</author>
//<email>mazq@tunynet.com</email>
//<log date="2008-2-21">创建</log>
//--------------------------------------------------------------
//</TunynetCopyright>

using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using Tunynet.QingdaoCustoms.Common;
using Tunynet.QingdaoCustoms.Data.Utils;
using Tunynet.QingdaoCustoms.Serialization;
using ChinaCustoms.Qingdao.Workflow.Supports.DataProvider;

namespace ChinaCustoms.Qingdao.Workflow.Supports
{
    /// <summary>
    /// 工作流实例数据提供者
    /// </summary>
    internal class WorkflowTrackingDataProvider
    {
        private static WorkflowTrackingDataProvider _instance = null;
        public static WorkflowTrackingDataProvider Instance()
        {
            if (_instance == null)
                _instance = new WorkflowTrackingDataProvider();

            return _instance;
        }

        #region WorkflowInstance

        /// <summary>
        /// 添加、更新工作流实例
        /// </summary>
        public void CreateUpdateWorkflow(WorkflowInstanceInfo workflowInstanceInfo, DataProviderAction action)
        {
            using (SqlConnection myConnection = GetSqlConnection())
            {
                SqlCommand myCommand = new SqlCommand("wf_WorkflowInstance_CreateUpdateDelete", myConnection);
                myCommand.CommandType = CommandType.StoredProcedure;

                myCommand.Parameters.Add("@Action", SqlDbType.Int).Value = (int)action;
                myCommand.Parameters.Add("@WorkflowGuid", SqlDbType.UniqueIdentifier).Value = workflowInstanceInfo.WorkflowGuid;
                myCommand.Parameters.Add("@Status", SqlDbType.Int).Value = (int)workflowInstanceInfo.Status;

                if (myConnection.State == ConnectionState.Closed)
                {
                    myConnection.Open();
                }
                myCommand.ExecuteNonQuery();
                myConnection.Close();
            }
        }

        /// <summary>
        /// 删除工作流实例
        /// </summary>
        public void DeleteWorkflow(Guid workflowGuid)
        {
            using (SqlConnection myConnection = GetSqlConnection())
            {
                SqlCommand myCommand = new SqlCommand("wf_WorkflowInstance_CreateUpdateDelete", myConnection);
                myCommand.CommandType = CommandType.StoredProcedure;

                myCommand.Parameters.Add("@Action", SqlDbType.Int).Value = (int)DataProviderAction.Delete;
                myCommand.Parameters.Add("@WorkflowGuid", SqlDbType.UniqueIdentifier).Value = workflowGuid;

                if (myConnection.State == ConnectionState.Closed)
                {
                    myConnection.Open();
                }
                myCommand.ExecuteNonQuery();
                myConnection.Close();
            }
        }

        /// <summary>
        /// 获取Workflow实例
        /// </summary>
        public WorkflowInstanceInfo GetWorkflow(Guid workflowGuid)
        {
            WorkflowInstanceInfo info = null;
            using (SqlConnection myConnection = GetSqlConnection())
            {
                SqlCommand myCommand = new SqlCommand("wf_WorkflowInstance_Get", myConnection);
                myCommand.CommandType = CommandType.StoredProcedure;

                myCommand.Parameters.Add("@WorkflowGuid", System.Data.SqlDbType.UniqueIdentifier).Value = workflowGuid;

                if (myConnection.State == ConnectionState.Closed)
                {
                    myConnection.Open();
                }
                using (SqlDataReader dr = myCommand.ExecuteReader(CommandBehavior.CloseConnection | CommandBehavior.SingleRow))
                {
                    if (dr.Read())
                    {
                        info = PopulateWorkflowInstanceInfoFromIDataReader(dr);
                    }
                }
                myConnection.Close();

                return info;
            }
        }


        #endregion

        #region ActivityInstance

        /// <summary>
        /// 添加Activity实例跟踪记录
        /// </summary>
        public bool CreateUpdateActivity(ActivityInstanceInfo activityInstanceInfo, DataProviderAction action)
        {
            using (SqlConnection myConnection = GetSqlConnection())
            {
                SqlCommand myCommand = new SqlCommand("wf_ActivityInstance_CreateUpdateDelete", myConnection);
                myCommand.CommandType = CommandType.StoredProcedure;

                myCommand.Parameters.Add("@Action", SqlDbType.Int).Value = (int)action;
                myCommand.Parameters.Add("@ActivityGuid", SqlDbType.UniqueIdentifier).Value = activityInstanceInfo.ActivityGuid;
                myCommand.Parameters.Add("@PrototypeGuid", SqlDbType.UniqueIdentifier).Value = activityInstanceInfo.PrototypeGuid;
                myCommand.Parameters.Add("@WorkflowGuid", SqlDbType.UniqueIdentifier).Value = activityInstanceInfo.WorkflowGuid;
                myCommand.Parameters.Add("@SubTaskGuid", SqlDbType.UniqueIdentifier).Value = activityInstanceInfo.SubTaskGuid;
                myCommand.Parameters.Add("@SubFlowGuid", SqlDbType.UniqueIdentifier).Value = activityInstanceInfo.SubFlowGuid;

                myCommand.Parameters.Add("@ActivityName", SqlDbType.NVarChar, 128).Value = activityInstanceInfo.ActivityName;
                myCommand.Parameters.Add("@CodeName", SqlDbType.NVarChar, 128).Value = activityInstanceInfo.CodeName;
                myCommand.Parameters.Add("@ActivityLevel", SqlDbType.Int).Value = activityInstanceInfo.ActivityLevel;
                myCommand.Parameters.Add("@Description", SqlDbType.NVarChar, 2000).Value = activityInstanceInfo.Description;
                myCommand.Parameters.Add("@UrlOfWorkItem", SqlDbType.NVarChar, 512).Value = activityInstanceInfo.UrlOfWorkItem;
                myCommand.Parameters.Add("@ActivityCodeNamesForJump", SqlDbType.NVarChar, 512).Value = activityInstanceInfo.ActivityCodeNamesForJump;
                myCommand.Parameters.Add("@AllowBack", SqlDbType.Bit).Value = activityInstanceInfo.AllowBack;
                myCommand.Parameters.Add("@AllowFinalJudgment", SqlDbType.Bit).Value = activityInstanceInfo.AllowFinalJudgment;
                myCommand.Parameters.Add("@IsSubFlow", SqlDbType.Bit).Value = activityInstanceInfo.IsSubFlow;
                myCommand.Parameters.Add("@RequiredDepartmentIDs", SqlDbType.NVarChar, 512).Value = activityInstanceInfo.RequiredDepartmentIDs;
                myCommand.Parameters.Add("@RequiredRelativeDepartment", SqlDbType.Int).Value = (int)activityInstanceInfo.RequiredRelativeDepartment;
                myCommand.Parameters.Add("@RequiredRoles", SqlDbType.NVarChar, 512).Value = activityInstanceInfo.RequiredRoles;
                myCommand.Parameters.Add("@RequiredUserGroups", SqlDbType.NVarChar, 512).Value = activityInstanceInfo.RequiredUserGroups;
                myCommand.Parameters.Add("@RequiredUserNames", SqlDbType.NVarChar, 1000).Value = activityInstanceInfo.RequiredUserNames;
                myCommand.Parameters.Add("@AllowRedesignateExecutor", SqlDbType.Bit).Value = activityInstanceInfo.AllowRedesignateExecutor;
                myCommand.Parameters.Add("@AllowPostil", SqlDbType.Bit).Value = (bool)activityInstanceInfo.AllowPostil;

                myCommand.Parameters.Add("@Status", SqlDbType.Int).Value = (int)activityInstanceInfo.Status;

                SerializerData serializerData = activityInstanceInfo.GetSerializerData();
                myCommand.Parameters.Add("@PropertyNames", SqlDbType.NText).Value = serializerData.Keys;
                myCommand.Parameters.Add("@PropertyValues", SqlDbType.NText).Value = serializerData.Values;

                if (myConnection.State == ConnectionState.Closed)
                {
                    myConnection.Open();
                }
                myCommand.ExecuteNonQuery();
                myConnection.Close();
            }
            return true;
        }

        /// <summary>
        /// 变更Activity状态
        /// </summary>
        public bool ChangeActivityStatus(Guid activityGuid, ActivityStatus activityStatus)
        {
            ActivityInstanceInfo activity = GetActivity(activityGuid);
            if (activity != null)
            {
                activity.Status = activityStatus;
                CreateUpdateActivity(activity, DataProviderAction.Update);
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 获取活动实例
        /// </summary>
        public ActivityInstanceInfo GetActivity(Guid activityGuid)
        {
            ActivityInstanceInfo info = null;

            using (SqlConnection myConnection = GetSqlConnection())
            {
                SqlCommand myCommand = new SqlCommand("wf_ActivityInstance_Get", myConnection);
                myCommand.CommandType = CommandType.StoredProcedure;
                myCommand.Parameters.Add("@ActivityGuid", System.Data.SqlDbType.UniqueIdentifier).Value = activityGuid;

                if (myConnection.State == ConnectionState.Closed)
                {
                    myConnection.Open();
                }

                using (SqlDataReader dr = myCommand.ExecuteReader(CommandBehavior.CloseConnection | CommandBehavior.SingleRow))
                {
                    if (dr.Read())
                    {
                        info = PopulateActivityInstanceInfoFromIDataReader(dr);
                    }
                }

                myConnection.Close();

                return info;
            }
        }

        /// <summary>
        /// 获取活动实例
        /// </summary>
        public ActivityInstanceInfo GetLastActivityByCodeName(Guid workflowGuid, Guid subTaskGuid, string codeName)
        {
            ActivityInstanceInfo info = null;

            using (SqlConnection myConnection = GetSqlConnection())
            {
                SqlCommand myCommand = new SqlCommand("wf_ActivityInstance_GetLastByCodeName", myConnection);
                myCommand.CommandType = CommandType.StoredProcedure;
                myCommand.Parameters.Add("@WorkflowGuid", System.Data.SqlDbType.UniqueIdentifier).Value = workflowGuid;
                myCommand.Parameters.Add("@SubTaskGuid", System.Data.SqlDbType.UniqueIdentifier).Value = subTaskGuid;

                myCommand.Parameters.Add("@CodeName", SqlDbType.NVarChar, 128).Value = codeName;

                if (myConnection.State == ConnectionState.Closed)
                {
                    myConnection.Open();
                }

                using (SqlDataReader dr = myCommand.ExecuteReader(CommandBehavior.CloseConnection | CommandBehavior.SingleRow))
                {
                    if (dr.Read())
                    {
                        info = PopulateActivityInstanceInfoFromIDataReader(dr);
                    }
                }

                myConnection.Close();

                return info;
            }
        }


        /// <summary>
        /// 获取工作流实例的所有活动
        /// </summary>
        public List<ActivityInstanceInfo> GetActivities(Guid workflowGuid, Guid? subTaskGuid)
        {
            List<ActivityInstanceInfo> list = new List<ActivityInstanceInfo>();

            using (SqlConnection myConnection = GetSqlConnection())
            {
                SqlCommand myCommand = new SqlCommand("wf_ActivityInstances_Get", myConnection);
                myCommand.CommandType = CommandType.StoredProcedure;
                myCommand.Parameters.Add("@WorkflowGuid", System.Data.SqlDbType.UniqueIdentifier).Value = workflowGuid;

                if (subTaskGuid.HasValue)
                    myCommand.Parameters.Add("@SubTaskGuid", System.Data.SqlDbType.UniqueIdentifier).Value = subTaskGuid;

                if (myConnection.State == ConnectionState.Closed)
                {
                    myConnection.Open();
                }

                using (SqlDataReader dr = myCommand.ExecuteReader(CommandBehavior.CloseConnection))
                {
                    while (dr.Read())
                    {
                        ActivityInstanceInfo info = PopulateActivityInstanceInfoFromIDataReader(dr);
                        list.Add(info);
                    }
                }

                myConnection.Close();

                return list;
            }
        }

        #endregion


        #region ActivityPrototype

        /// <summary>
        /// 添加更新活动原型
        /// </summary>
        public void CreateUpdateActivityPrototype(ActivityPrototypeInfo activityPrototypeInfo, DataProviderAction action)
        {
            using (SqlConnection myConnection = GetSqlConnection())
            {
                SqlCommand myCommand = new SqlCommand("wf_ActivityPrototype_CreateUpdate", myConnection);
                myCommand.CommandType = CommandType.StoredProcedure;

                myCommand.Parameters.Add("@Action", SqlDbType.Int).Value = (int)action;
                myCommand.Parameters.Add("@PrototypeGuid", SqlDbType.UniqueIdentifier).Value = activityPrototypeInfo.PrototypeGuid;
                myCommand.Parameters.Add("@WorkflowGuid", SqlDbType.UniqueIdentifier).Value = activityPrototypeInfo.WorkflowGuid;
                myCommand.Parameters.Add("@SubFlowGuid", SqlDbType.UniqueIdentifier).Value = activityPrototypeInfo.SubFlowGuid;
                myCommand.Parameters.Add("@ActivityName", SqlDbType.NVarChar, 128).Value = activityPrototypeInfo.ActivityName;
                myCommand.Parameters.Add("@CodeName", SqlDbType.NVarChar, 128).Value = activityPrototypeInfo.CodeName;
                myCommand.Parameters.Add("@ActivityLevel", SqlDbType.Int).Value = activityPrototypeInfo.ActivityLevel;
                myCommand.Parameters.Add("@Description", SqlDbType.NVarChar, 2000).Value = activityPrototypeInfo.Description;
                myCommand.Parameters.Add("@UrlOfWorkItem", SqlDbType.NVarChar, 512).Value = activityPrototypeInfo.UrlOfWorkItem;
                myCommand.Parameters.Add("@ActivityCodeNamesForJump", SqlDbType.NVarChar, 512).Value = activityPrototypeInfo.ActivityCodeNamesForJump;
                myCommand.Parameters.Add("@AllowBack", SqlDbType.Bit).Value = activityPrototypeInfo.AllowBack;
                myCommand.Parameters.Add("@AllowFinalJudgment", SqlDbType.Bit).Value = activityPrototypeInfo.AllowFinalJudgment;
                myCommand.Parameters.Add("@IsSubFlow", SqlDbType.Bit).Value = activityPrototypeInfo.IsSubFlow;
                myCommand.Parameters.Add("@RequiredDepartmentIDs", SqlDbType.NVarChar, 512).Value = activityPrototypeInfo.RequiredDepartmentIDs;
                myCommand.Parameters.Add("@RequiredRelativeDepartment", SqlDbType.Int).Value = (int)activityPrototypeInfo.RequiredRelativeDepartment;
                myCommand.Parameters.Add("@RequiredRoles", SqlDbType.NVarChar, 512).Value = activityPrototypeInfo.RequiredRoles;
                myCommand.Parameters.Add("@RequiredUserGroups", SqlDbType.NVarChar, 512).Value = activityPrototypeInfo.RequiredUserGroups;
                myCommand.Parameters.Add("@RequiredUserNames", SqlDbType.NVarChar, 1000).Value = activityPrototypeInfo.RequiredUserNames;
                myCommand.Parameters.Add("@AllowRedesignateExecutor", SqlDbType.Bit).Value = activityPrototypeInfo.AllowRedesignateExecutor;
                myCommand.Parameters.Add("@AllowPostil", SqlDbType.Bit).Value = (bool)activityPrototypeInfo.AllowPostil;
                //myCommand.Parameters.Add("@AllowAttachment", SqlDbType.Bit).Value = (bool)activityPrototypeInfo.AllowAttachment;

                SerializerData serializerData = activityPrototypeInfo.GetSerializerData();
                myCommand.Parameters.Add("@PropertyNames", SqlDbType.NText).Value = serializerData.Keys;
                myCommand.Parameters.Add("@PropertyValues", SqlDbType.NText).Value = serializerData.Values;

                if (myConnection.State == ConnectionState.Closed)
                {
                    myConnection.Open();
                }
                myCommand.ExecuteNonQuery();
                myConnection.Close();
            }
        }

        /// <summary>
        /// 获取活动原型
        /// </summary>
        public ActivityPrototypeInfo GetActivityPrototype(Guid prototypeGuid)
        {
            ActivityPrototypeInfo info = null;
            using (SqlConnection myConnection = GetSqlConnection())
            {
                SqlCommand myCommand = new SqlCommand("wf_ActivityPrototype_Get", myConnection);
                myCommand.CommandType = CommandType.StoredProcedure;

                myCommand.Parameters.Add("@PrototypeGuid", System.Data.SqlDbType.UniqueIdentifier).Value = prototypeGuid;

                if (myConnection.State == ConnectionState.Closed)
                {
                    myConnection.Open();
                }
                using (SqlDataReader dr = myCommand.ExecuteReader(CommandBehavior.CloseConnection | CommandBehavior.SingleRow))
                {
                    if (dr.Read())
                    {
                        info = PopulateActivityPrototypeInfoFromIDataReader(dr);
                    }
                }
                myConnection.Close();

                return info;
            }
        }

        /// <summary>
        /// 获取工作流实例的所有活动原型
        /// </summary>
        public List<ActivityPrototypeInfo> GetActivityPrototypes(Guid workflowGuid)
        {
            List<ActivityPrototypeInfo> list = new List<ActivityPrototypeInfo>();
            using (SqlConnection myConnection = GetSqlConnection())
            {
                SqlCommand myCommand = new SqlCommand("wf_ActivityPrototypes_GetByWorkflowGuid", myConnection);
                myCommand.CommandType = CommandType.StoredProcedure;

                myCommand.Parameters.Add("@WorkflowGuid", SqlDbType.UniqueIdentifier).Value = workflowGuid;

                if (myConnection.State == ConnectionState.Closed)
                {
                    myConnection.Open();
                }
                using (SqlDataReader dr = myCommand.ExecuteReader(CommandBehavior.CloseConnection))
                {
                    while (dr.Read())
                    {
                        ActivityPrototypeInfo info = PopulateActivityPrototypeInfoFromIDataReader(dr);
                        list.Add(info);
                    }
                }
                myConnection.Close();

                return list;
            }
        }

        #endregion


        #region SubFlow

        /// <summary>
        /// 添加更新活动原型
        /// </summary>
        public void CreateUpdateSubFlow(SubFlowInfo subFlow, DataProviderAction action)
        {
            using (SqlConnection myConnection = GetSqlConnection())
            {
                SqlCommand myCommand = new SqlCommand("wf_SubFlow_CreateUpdate", myConnection);
                myCommand.CommandType = CommandType.StoredProcedure;

                myCommand.Parameters.Add("@Action", SqlDbType.Int).Value = (int)action;
                myCommand.Parameters.Add("@SubFlowGuid", SqlDbType.UniqueIdentifier).Value = subFlow.SubFlowGuid;
                myCommand.Parameters.Add("@WorkflowGuid", SqlDbType.UniqueIdentifier).Value = subFlow.WorkflowGuid;
                myCommand.Parameters.Add("@ActivityPrototypeGuid", SqlDbType.UniqueIdentifier).Value = subFlow.ActivityPrototypeGuid;
                myCommand.Parameters.Add("@SubFlowName", SqlDbType.NVarChar, 128).Value = subFlow.SubFlowName;

                if (myConnection.State == ConnectionState.Closed)
                {
                    myConnection.Open();
                }
                myCommand.ExecuteNonQuery();
                myConnection.Close();
            }
        }

        /// <summary>
        /// 获取活动原型
        /// </summary>
        public SubFlowInfo GetSubFlow(Guid? subFlowGuid, Guid? activityPrototypeGuid)
        {
            SubFlowInfo info = null;
            using (SqlConnection myConnection = GetSqlConnection())
            {
                SqlCommand myCommand = new SqlCommand("wf_SubFlow_Get", myConnection);
                myCommand.CommandType = CommandType.StoredProcedure;

                if (subFlowGuid.HasValue)
                    myCommand.Parameters.Add("@SubFlowGuid", System.Data.SqlDbType.UniqueIdentifier).Value = subFlowGuid;

                if (activityPrototypeGuid.HasValue)
                    myCommand.Parameters.Add("@ActivityPrototypeGuid", System.Data.SqlDbType.UniqueIdentifier).Value = activityPrototypeGuid;

                if (myConnection.State == ConnectionState.Closed)
                {
                    myConnection.Open();
                }
                using (SqlDataReader dr = myCommand.ExecuteReader(CommandBehavior.CloseConnection | CommandBehavior.SingleRow))
                {
                    if (dr.Read())
                    {
                        info = PopulateSubFlowInfoFromIDataReader(dr);
                    }
                }
                myConnection.Close();

                return info;
            }
        }

        #endregion


        #region Helper methods & properties

        private SqlConnection GetSqlConnection()
        {
            try
            {
                //return new SqlConnection(SqlHelper.WorkflowConnectionString);
                return SqlHelper.WorkflowConnection;
            }
            catch
            {
                throw new Exception("SQL Connection String is invalid.");
            }
        }

        /// <summary>
        /// 通过IDataReader组装WorkflowInstanceInfo
        /// </summary>
        private WorkflowInstanceInfo PopulateWorkflowInstanceInfoFromIDataReader(IDataReader dr)
        {
            WorkflowInstanceInfo workflowTrackingInfo = new WorkflowInstanceInfo();
            workflowTrackingInfo.WorkflowGuid = new Guid(Convert.ToString(dr["WorkflowGuid"]));
            workflowTrackingInfo.Status = (WorkflowStatus)Convert.ToInt32(dr["Status"]);
            workflowTrackingInfo.CreateDate = Convert.ToDateTime(dr["CreateDate"]);
            return workflowTrackingInfo;
        }

        /// <summary>
        /// 通过IDataReader组装ActivityInstanceInfo
        /// </summary>
        private ActivityInstanceInfo PopulateActivityInstanceInfoFromIDataReader(IDataReader dr)
        {
            ActivityInstanceInfo info = new ActivityInstanceInfo();
            info.ActivityGuid = new Guid(Convert.ToString(dr["ActivityGuid"]));

            info.PrototypeGuid = new Guid(Convert.ToString(dr["PrototypeGuid"]));
            info.WorkflowGuid = new Guid(Convert.ToString(dr["WorkflowGuid"]));
            info.SubTaskGuid = new Guid(Convert.ToString(dr["SubTaskGuid"]));
            info.SubFlowGuid = new Guid(Convert.ToString(dr["SubFlowGuid"]));
            info.ActivityName = Convert.ToString(dr["ActivityName"]);
            info.CodeName = Convert.ToString(dr["CodeName"]);
            info.ActivityLevel = Convert.ToInt32(dr["ActivityLevel"]);
            info.Description = Convert.ToString(dr["Description"]);
            info.UrlOfWorkItem = Convert.ToString(dr["UrlOfWorkItem"]);
            info.ActivityCodeNamesForJump = Convert.ToString(dr["ActivityCodeNamesForJump"]);
            info.AllowBack = Convert.ToBoolean(dr["AllowBack"]);
            info.AllowFinalJudgment = Convert.ToBoolean(dr["AllowFinalJudgment"]);
            info.IsSubFlow = Convert.ToBoolean(dr["IsSubFlow"]);
            info.RequiredDepartmentIDs = Convert.ToString(dr["RequiredDepartmentIDs"]);
            info.RequiredRelativeDepartment = (RelativeDepartmentLevel)Convert.ToInt32(dr["RequiredRelativeDepartment"]);
            info.RequiredRoles = Convert.ToString(dr["RequiredRoles"]);
            info.RequiredUserGroups = Convert.ToString(dr["RequiredUserGroups"]);
            info.RequiredUserNames = Convert.ToString(dr["RequiredUserNames"]);
            info.AllowRedesignateExecutor = Convert.ToBoolean(dr["AllowRedesignateExecutor"]);
            info.AllowPostil = Convert.ToBoolean(dr["AllowPostil"]);
            //info.AllowAttachment = Convert.ToBoolean(dr["AllowAttachment"]);

            info.Status = (ActivityStatus)dr["Status"];
            info.CreateDate = Convert.ToDateTime(dr["CreateDate"]);

            SerializerData serializerData = DataProviderHelper.PopulateSerializerDataFromDataReader(dr);
            info.SetSerializerData(serializerData);

            return info;
        }

        /// <summary>
        /// 通过IDataReader组装ActivityPrototypeInfo
        /// </summary>
        private ActivityPrototypeInfo PopulateActivityPrototypeInfoFromIDataReader(IDataReader dr)
        {
            ActivityPrototypeInfo info = new ActivityPrototypeInfo();
            info.PrototypeGuid = new Guid(Convert.ToString(dr["PrototypeGuid"]));
            info.WorkflowGuid = new Guid(Convert.ToString(dr["WorkflowGuid"]));
            info.SubFlowGuid = new Guid(Convert.ToString(dr["SubFlowGuid"]));
            info.ActivityName = Convert.ToString(dr["ActivityName"]);
            info.CodeName = Convert.ToString(dr["CodeName"]);
            info.ActivityLevel = Convert.ToInt32(dr["ActivityLevel"]);
            info.Description = Convert.ToString(dr["Description"]);
            info.UrlOfWorkItem = Convert.ToString(dr["UrlOfWorkItem"]);
            info.ActivityCodeNamesForJump = Convert.ToString(dr["ActivityCodeNamesForJump"]);
            info.AllowBack = Convert.ToBoolean(dr["AllowBack"]);
            info.AllowFinalJudgment = Convert.ToBoolean(dr["AllowFinalJudgment"]);
            info.IsSubFlow = Convert.ToBoolean(dr["IsSubFlow"]);
            info.RequiredDepartmentIDs = Convert.ToString(dr["RequiredDepartmentIDs"]);
            info.RequiredRelativeDepartment = (RelativeDepartmentLevel)Convert.ToInt32(dr["RequiredRelativeDepartment"]);
            info.RequiredRoles = Convert.ToString(dr["RequiredRoles"]);
            info.RequiredUserNames = Convert.ToString(dr["RequiredUserNames"]);
            info.AllowRedesignateExecutor = Convert.ToBoolean(dr["AllowRedesignateExecutor"]);
            info.AllowPostil = Convert.ToBoolean(dr["AllowPostil"]);
            //info.AllowAttachment = Convert.ToBoolean(dr["AllowAttachment"]);

            SerializerData serializerData = DataProviderHelper.PopulateSerializerDataFromDataReader(dr);
            info.SetSerializerData(serializerData);

            return info;
        }

        /// <summary>
        /// 通过IDataReader组装SubFlowInfo
        /// </summary>
        protected SubFlowInfo PopulateSubFlowInfoFromIDataReader(IDataReader dr)
        {
            SubFlowInfo subFlow = new SubFlowInfo();
            subFlow.ActivityPrototypeGuid = new Guid(Convert.ToString(dr["ActivityPrototypeGuid"]));
            subFlow.SubFlowGuid = new Guid(Convert.ToString(dr["SubFlowGuid"]));
            subFlow.WorkflowGuid = new Guid(Convert.ToString(dr["WorkflowGuid"]));
            subFlow.SubFlowName = Convert.ToString(dr["SubFlowName"]);
            return subFlow;
        }

        #endregion
    }
}
